<?php

class HomeController extends BaseController {

  private function _getAllData() {
    Profiler::startTimer('StartGetAllData');
    $data               = array();
    $data['pagecount']  = 0;
    $data['blockcount'] = 0;
    $data['linkcount']  = 0;
    foreach (Auth::user()->pages()->with(array('blocks.links' => function($q) {
          $q->orderBy('order', 'ASC');
        }
    ))->orderBy('order', 'ASC')->get() as $pi => $page) {
      $data['pagecount']++;
      $data['pages'][$pi]           = $page->toArray();
      $data['pages'][$pi]['blocks'] = array();
      foreach ($page->blocks as $bi => $block) {
        $data['blockcount']++;
        $data['pages'][$pi]['blocks'][$bi]          = $block->toArray();
        $data['pages'][$pi]['blocks'][$bi]['links'] = array();
        foreach ($block->links as $li => $link) {
          $data['linkcount']++;
          $data['pages'][$pi]['blocks'][$bi]['links'][$li] = $link->toArray();
        }
      }
    }
    Profiler::endTimer('StartGetAllData');
    return $data;
  }

  public function getData() {
    return Response::json($this->_getAllData());
  }

  public function showHome() {
    Profiler::startTimer('showHome');
    Profiler::startTimer('getCache');
    $active = Cache::section(Auth::user()->email)->remember('active', 1440, function() {
              Log::debug('New active created');
              return Auth::user()->pages()->orderBy('order', 'ASC')->first();
            });
    $data = Cache::section(Auth::user()->email)->remember('data', 1440, function() {
      Log::debug('New data created');
              return $this->_getAllData();
            });
    $quote = Cache::section(Auth::user()->email)->remember('quote', 1440, function() {
              $quotes = Quote::orderBy('id', 'DESC')->take(4)->get();
              $rand   = rand(0, 3);
              if (isset($quotes[$rand])) {
                $quote = $quotes[$rand]; // lame!
                $txt   = $quote->quote . '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;~&nbsp;' . $quote->author;
              } else {
                $txt = '';
              }
              Log::debug('New quote created');
              return $txt;
            });
    Profiler::endTimer('getCache');

    Profiler::startTimer('getWarnings');
    if ($data['pagecount'] === 0) {
      Session::flash('warning', 'Add a new tab using Tools &rarr; Add tab.');
    }
    if ($data['pagecount'] > 0 && $data['blockcount'] === 0) {
      Session::flash('warning', 'Now, add a block using Tools &rarr; Add block. Don\'t forget to drag it around!');
    }

    if ($data['pagecount'] > 0 && $data['blockcount'] > 0 && $data['linkcount'] === 0) {
      Session::flash('warning', 'Add some links using Tools &rarr; Add link.');
    }
    Profiler::endTimer('getWarnings');

    Profiler::endTimer('showHome');
    Profiler::startTimer('renderHome');
    return View::make('home')->with('active', $active)->with('data', $data)->with('quote', $quote);
  }

  public function flush() {
    Cache::section(Auth::user()->email)->flush();
    return Redirect::to('/');
  }

}